// pages/myPan/mycollection/mycollection.js
import api from '../../../api/api.js'
import load from '../../templates/loader/loader.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    winHeight: "", //窗口高度 
    currentTab: 0, //预设当前项的值
    collectionItem: [],
    item: [{
      "data": [],
      "index": 0,
      "pageSize": 5,
      "pageNo": 1,
      "load": true
    }, {
      "data": [],
      "index": 1,
      "pageSize": 5,
      "pageNo": 1,
      "load": true
    }],
    init: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.loader = load.loader(this, {
      size: 50,
      borderWidth: 3
    });
    that.loader2 = load.loader(that, {
      data: "loader2",
      toast: false,
      borderWidth: 1
    });
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 100;
        that.setData({
          winHeight: calc
        });
      }
    });
    that.loader.open()
    //获取用户信息
    api.getUser({
      done: (user) => {
        that.data.userInfo=user
        that.getCollectionList(0)
      }
    })
  },
  onShow: function () {
    var that = this
    if (that.data.init) {
      //删除收藏
      wx.getStorage({
        key: 'collectionMsg',
        success: function (res) {
          that.data.collectionItem.forEach(function (item, index) {
            item.data.forEach(function (item_, index_) {
              if (item_.shop_id == res.data[index].id) {
                //item_.splice(index_, 1)
                that.data.collectionItem[index].data.splice(index_, 1)
              }
            });
          });
          // that.data.collectionItem[index].data.forEach(function(item,index_){
          //   if (item.shop_id==res.data[index].id){
          //     that.data.collectionItem[index].data.splice(index_, 1)
          //   }
          // })
          that.setData({
            collectionItem: that.data.collectionItem
          })
          wx.removeStorage({
            key: 'collectionMsg'
          })
        },
      })
    }
  },
  // 滚动切换标签样式
  switchTab: function (e) {
    this.setData({
      currentTab: e.detail.current
    });
    //判断是否已经加载过了
    if (this.data.collectionItem.length == 0) {
      this.getCollectionList()
    } else if (this.data.collectionItem[e.detail.current].data.length == 0) {
      this.getCollectionList()
    }
  },
  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    var cur = e.target.dataset.current;
    if (this.data.currentTaB == cur) {
      return false;
    } else {
      this.setData({
        currentTab: cur
      })
    }
  },
  getCollectionList: function () {
    var that = this
    var index = that.data.currentTab
    that.loader2.open()
    var json = '{"REQ_HEAD":{"SIGN":"null"},"REQ_BODY":{"userId":' + that.data.userInfo.id + ',"type":"' + index + '","pageSize":' + that.data.item[index].pageSize + ' ,"pageNo":' + that.data.item[index].pageNo + '}}';
    //判断当前页载入状态
    if (that.data.item[index].load) {
      that.data.item[index].load = false
      api.getCollectionList({
        method: 'POST',
        data: {
          REQ_MESSAGE: json
        },
        success: (res) => {
          that.loader.close()
          that.loader2.close(); //关闭载入动画
          that.data.item[index].load = true
          that.data.init = true;
          var data = res.rows;
          if (JSON.stringify(data) == "[]" || !data) {
            that.loader2.open("没有更多收藏了")
          } else if (data.length > 0) {
            that.data.item[index].data.length <= 0 ? that.data.item[index].data = data : that.data.item[index].data = that.data.item[index].data.concat(data)
            that.setData({
              collectionItem: that.data.item
            })
            that.data.item[index].pageNo++
          }

        },
        fail: (res) => {
          //that.loader.open(api.statusFail.title);
        }
      })
    }
  },
  onScrollBottom: function () {
    this.getCollectionList()
  }
})